<?php

class Jnd_Environment extends Zend_Session_Namespace
{

    /**
     *
     * @var Zend_Session_Namespace
     */
    protected $_stockage;

    /**
     *
     * @property string societe
     */
    final public function __construct (array $options = NULL)
    {
        $this->_stockage = new Zend_Session_Namespace(get_class($this));
        if ($options !== null) {
            $this->setOptions($options);
        }
        $this->init();
    }

    public function init ()
    {}

    public function __set ($name, $value)
    {
        if (substr($name, 0, 1) == "_") {
            throw new Exception(
                    "Modification de variable protégée non autorisée");
        }
        $this->_stockage->$name = $value;
    }

    public function __get ($name)
    {
        return $this->_stockage->$name;
    }
    
    //
    // public function __isset($name) {
    // return isset($this->_stockage->$name);
    // }
    public function has ($name)
    {
        return isset($this->_stockage->$name);
    }

    public function setOption ($key, $value)
    {
        $method = "set" . ucfirst($key);
        if (method_exists($this, $method)) {
            $this->$method($value);
        } else {
            throw new Exception("Option ($key) inexistante");
        }
    }

    public function setOptions (array $options)
    {
        foreach ($options as $key => $value) {
            $this->setOption($key, $value);
        }
    }

    public static function factory ($appli, array $options = NULL)
    {
        $className = ucfirst(strtolower($appli)) . '_Environment';
        return new $className($options);
    }

    public function setRole ($role)
    {
        $this->role = $role;
    }

    public function getRole ()
    {
        return $this->role;
    }

    public function setSociete ($societe)
    {
        $this->societe = $societe;
    }

    public function getSociete ()
    {
        return $this->societe;
    }

    /**
     * Renvoie l'utilisateur connecté à l'application
     *
     * @return Jnd_User|Jnd_User_Model
     */
    public function getUtilisateurCourant ()
    {
        if (Zend_Auth::getInstance()->getIdentity() != "") {
            $user = Jnd_User::factory("Model", 
                    Zend_Auth::getInstance()->getIdentity());
            return $user->getUtilisateur();
        }
    }

    public function resetInstance ()
    {
        $session = $this->_stockage;
        if (! is_null($session) && $session instanceof Zend_Session_Namespace) {
            $session->unsetAll();
        }
        if (! is_null($this->_stockage) &&
                 $this->_stockage instanceof Zend_Session_Namespace) {
            $this->_stockage->resetSingleInstance(get_class($this));
            $this->_stockage = null;
        }
        
        // $session->resetSingleInstance(self::WGC_ENVIRONNEMENT);
        // $this->environnement = null;
    }

    public static function estExterne ()
    {
        $clientIp = $_SERVER['REMOTE_ADDR'];
        $matches = array();
        preg_match('/^192.168/', $clientIp, $matches);
        if (count($matches) > 0) {
            return false;
        } else {
            return true;
        }
    }

    public function setException (Exception $e)
    {
        $this->_stockage->exception = $e;
    }

    public function getException ()
    {
        $exception = $this->_stockage->exception;
        $this->_stockage->excetpion = null;
        return $exception;
    }
}

